var curSetting = {};
$(function() {

	$.getJSON("admin/department/department!gets", {
		"bean.id" : "root"
	}, function(data) {

		$.each(data, function(i, item) {
			var opt = $("<option value='" + item.id + "'>" + item.name+ "</option>");
			$("#depts").append(opt);
		});
		$("#depts").bind("multiselectclick", function(event, ui) {
			/*
			 * event: the original event object
			 * 
			 * ui.value: value of the checkbox ui.text: text of the checkbox
			 * ui.checked: whether or not the input was checked or unchecked
			 * (boolean)
			 */
		}).multiselect({
			selectedText : "选择了#个总数  #",
			noneSelectedText : '请选择部门',
			selectedList : 4,
			checkAllText : "选择所有",
			uncheckAllText : "取消所有",
			autoOpen : true
		}).multiselectfilter();
	});

	$.getJSON("admin/role/role!gets",

	function(data) {

		$.each(data, function(i, item) {
			var opt = $("<option value='" + item.id + "'>" + item.name
					+ "</option>");
			$("#roles").append(opt);
		});
		$("#roles").bind("multiselectclick", function(event, ui) {
			/*
			 * event: the original event object
			 * 
			 * ui.value: value of the checkbox ui.text: text of the checkbox
			 * ui.checked: whether or not the input was checked or unchecked
			 * (boolean)
			 */
		}).multiselect({
			selectedText : "选择了#个总数  #",
			noneSelectedText : '请选择角色',
			selectedList : 4,
			checkAllText : "选择所有",
			uncheckAllText : "取消所有",
			autoOpen : true,
			multiple:true
		}).multiselectfilter();
	});

	$("#saveBtn").click(
			function() {
				var ps = '';
				var x = 0;				
				for ( var s = 0; s < sets.length; s++) {
					if (sets[s] == null)
						continue;
					for ( var a = 0; a < sets[s].length; a++) {
						var st = sets[s][a];
						if (st == null)
							continue;
						if (st.user != null) {
							x++;
							ps += "&beans[" + x + "].user=${" + st.user + "}";
							ps += "&beans[" + x + "].id.name=" + workflowName;
							ps += "&beans[" + x + "].id.stepId=" + s;
							ps += "&beans[" + x + "].id.actionId=" + a;
							continue;
						}
						if (st.departments.length != 0) {
							x++;
							ps += "&beans[" + x + "].id.name=" + workflowName;
							ps += "&beans[" + x + "].id.stepId=" + s;
							ps += "&beans[" + x + "].id.actionId=" + a;
							for ( var d = 0; d < st.departments.length; d++) {
								var dpt = st.departments[d];
								if (dpt == null)
									continue;
								ps += "&beans[" + x + "].departments[" + d
										+ "].id=" + dpt.id;
							}
							continue;
						}
						if(typeof st.roles.length != 0){
							x++;
							ps += "&beans[" + x + "].id.name=" + workflowName;
							ps += "&beans[" + x + "].id.stepId=" + s;
							ps += "&beans[" + x + "].id.actionId=" + a;
							for ( var r = 0; r < st.roles.length; r++) {
								var rpt = st.roles[r];
								if (rpt == null)
									continue;
								ps += "&beans[" + x + "].roles[" + r
										+ "].id=" + rpt.id;
							}					
							
							continue;
							
						}

					}
				}
				var url = "workflow/setting/workflow-setting!add";
				//alert(ps);
				doSubmit(url, ps);

			}).button();

	$("#deptSelector").bind("dialogopen", function(event, ui) {
		$("#depts").multiselect("uncheckAll");
		$("#depts").multiselect("open");
	}).dialog(
			{
				autoOpen : false,
				modal : true,
				height : 380,
				buttons : {
					"确定" : function() {
						var depts = $("#depts").multiselect("getChecked").map(
								function() {
									return this.value;
								}).get();
						$.each(depts, function(idx, deptId) {
							curSetting.departments.push({
								"id" : deptId
							});
						});
						$(this).dialog("close");

					}
				}
	});
	$("#roleSelector").bind("dialogopen", function(event, ui) {
		$("#roles").multiselect("uncheckAll");
		$("#roles").multiselect("open");
	}).dialog(
			{
				autoOpen : false,
				modal : true,
				height : 380,
				buttons : {
					"确定" : function() {
						var roles = $("#roles").multiselect("getChecked").map(
								function() {
									return this.value;
								}).get();
						
						$.each(roles, function(idx, roleId) {
							curSetting.roles.push({
								"id" : roleId
							});
						});										
						$(this).dialog("close");						
					}
				}
			}
			
	);
	
	
});
function openDeptSelector(step, action) {
	setStepOwner(step, action, null);
	$("#deptSelector").dialog("open");
}
/**
 * 设置人员选择器
 * 
 * @param step
 * @param action
 * @param userexp
 */
function setStepOwner(step, action, userexp) {
	curSetting = sets[step][action];
	curSetting.user = userexp;
	// debug.html(toJsonString(sets));
}
/** 设置人员选择器 */
function setDeptSelector(step, action) {
	setStepOwner(step, action, null);
};

function openRoleSelector(step, action) {
	setStepOwner(step, action, null);
	
	$("#roleSelector").dialog("open");
}

function setVarName(step,action,obj){
	var tid = "#specOwner"+(step*action);
	var chk = $(tid).attr("checked");
	if(chk){		
		var name = $(obj).val();		
		setStepOwner(step,action,name);
	}
	
}